package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/description/
 * @create 2025/7/31 20:18
 * @since JDK17
 */

public class Solution20 {
    public long countSubarrays(int[] nums, long k) {
        long ans = 0, sum = 0, cnt = 0;
        int n = nums.length;

        for (int l = 0, r = 0; r < n; r++) {
            sum += nums[r];
            cnt++;
            while (sum * cnt >= k) {
                cnt--;
                sum -= nums[l++];
            }
            ans += r - l + 1;
        }
        return ans;
    }
}
